package com.serkol.ams.bean;

import com.serkol.ams.entity.PosterDevise;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class ImageInfoRect {
    private List<Map<String, Object>> pics;
    private List<Map<String, Object>> texts;




    public ImageInfoRect() {
        pics = new java.util.ArrayList<>();
        texts = new java.util.ArrayList<>();

    }

    public void posterDevise(String url) {
        ImageInfoRectItem posterDevise = new ImageInfoRectItem();
        posterDevise.setSrc(url);
        posterDevise.setX(0);
        posterDevise.setY(0);
        posterDevise.setW(750);
        posterDevise.setH(1334);
        posterDevise.setPreload(true);
        if(StringUtils.isNotEmpty(url)) {
            pics.add(getMapItem(posterDevise));
        }
    }
    public void  add(PosterDevise item, ImageInfo info) {
        if (item.getType().equals("true")) {
            ImageInfoRectItem e = new ImageInfoRectItem();
            e.setX(item.getX());
            e.setY(item.getY());
            e.setW(item.getW());
            e.setH(item.getH());
            e.setR(item.getRadius());
            switch (item.getKeyName()) {
                case "goodsImage":
                    e.setSrc(info.getGoodsImage());
                    break;
                case "posterImage":
                    e.setPreload(true);
                    e.setSrc(info.getPosterImage());
                    break;
                case "qrcode":
                    e.setSrc(info.getQrcode());
                    break;
                case "avatar":
                    e.setR(140);
                    e.setSrc(info.getAvatar());
                    break;
            }
            if(StringUtils.isNotEmpty(e.getSrc())) {
                pics.add(getMapItem(e));
            }

        }
        if (item.getType().equals("false")) {
            ImageInfoRectItem e = new ImageInfoRectItem();
            e.setX(item.getX());
            e.setY(item.getY());
            e.setW(item.getW());
            e.setH(item.getH());
            e.setR(item.getRadius());
            e.setColor(item.getColor());
            e.setFont(item.getFontSize()+"px Arail");
            switch (item.getKeyName()) {
                case "title":
                    e.setText(info.getTitle());
                    break;
                case "goodName":
                    e.setText(info.getGoodName());
                    break;
                case "authName":
                    e.setText(info.getAuthName());
                    break;
                case "price":
                    if(info.getPrice() == null) {
                        e.setText("0.00");
                    }else {
                        e.setText(info.getPrice().toString());
                    }

                    break;
                case "invalid":
                   e.setText(info.getInvalid());
            }
            if(StringUtils.isNotEmpty(e.getText())) {
                texts.add(getMapItem(e));
            }
        }
    }

    private Map<String, Object> getMapItem(ImageInfoRectItem e) {
        Map<String, Object> map = new HashMap<>();

        if (e != null) {
            if (e.getType() != null) {
                map.put("type", e.getType());
            }
            if (e.getSrc() != null) {
                map.put("src", e.getSrc());
            }
            if (e.isPreload()) {
                map.put("preload", true);
            }
            if (e.getAlgin() != null) {
                map.put("algin", e.getAlgin());
            }
            if (e.getColor() != null) {
                map.put("color", e.getColor());
            }
            if (e.getFont() != null) {
                map.put("font", e.getFont());
            }
            if (e.getText() != null) {
                map.put("text", e.getText());
            }
            if (e.getR() != null) {
                map.put("r", e.getR());
            }
            if (e.getX() != null) {
                map.put("x", e.getX());
            }
            if (e.getY() != null) {
                map.put("y", e.getY());
            }
            if (e.getW() != null) {
                map.put("w", e.getW());
            }
            if (e.getH() != null) {
                map.put("h", e.getH());
            }

        }

        return map;
    }



}
